package com.yand.serviceimpl.business.custom;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yand.api.entity.business.custom.THfStudent;
import com.yand.api.service.business.custom.StudentService;
import com.yand.mapper.business.custom.THfStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 学生业务逻辑类
 * @author  yanzz
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private THfStudentMapper studentMapper;


    /**
     * 查询所有学生列表
     * @param student
     * @return
     */
    @Override
    public PageInfo<THfStudent> queryAllStudentList(THfStudent student) {
        PageHelper.startPage(student.getPageNum(), student.getPageSize());

        return PageInfo.of(studentMapper.selectAllStudentList(student));
    }

    /**
     * 通过studentid 获取student对象信息
     * @param studentid
     * @return
     */
    @Override
    public THfStudent getStudentBean(Long studentid) {
        THfStudent student = new THfStudent();
        if(studentid == 0L){
            return student;
        }

        student.setId(Integer.parseInt(studentid.toString()));
        return studentMapper.selectAllStudentList(student).get(0);
    }

    /**
     * 添加学生信息
     * @param student
     */
    @Override
    public void addStudent(THfStudent student) {
        student.setExtand1("0");
        studentMapper.insertSelective(student);
    }

    /**
     * 删除学生
     * @param studentId
     */
    @Override
    public void delStudent(Integer studentId) {
        studentMapper.deleteByPrimaryKey(studentId);
    }

    /**
     * 修改学生
     * @param student
     */
    @Override
    public void updateStudent(THfStudent student) {
        studentMapper.updateByPrimaryKeySelective(student);
    }

    /**
     * 修改学生状态
     * @param studentId
     * @param status
     */
    @Override
    public void updateStudentStatus(String studentId, String status) {
        Map<String, Object> param = new HashMap<>();
        param.put("studentId",studentId);
        param.put("status",status);
        studentMapper.updateStudentStatus(param);
    }


    /**
     * 获取学生名
     * @param studentIds
     * @return
     */
    @Override
    public String getStudentName(String studentIds) {
        String [] studentIdArray = studentIds.split(",");
        String studentName = "";
        THfStudent student = null;
        for(int i = 0 ; i < studentIdArray.length; i ++){
             student =  getStudentBean(Long.valueOf(studentIdArray[i]));
             if("".equals(studentName)){
                 studentName = student.getStudentName();
             }else{
                 studentName += ","+student.getStudentName();
             }
        }

        return studentName;
    }
}
